﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_CarbonFootprintQuestion_Waste_HouseholdWaste : CarbonExchange.WebControls.AuditQuestionSigleItemBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public override void DataBind()
    {
        base.DataBind();
        ddlHowMuch.Text = Answer.GetSetting<string>("ddlHowMuch");
        txtHowMuch.Text = Answer.GetSetting<string>("txtHowMuch");
    }

    protected override bool SaveQuestion()
    {
        Answer.SetSetting("ddlHowMuch", ddlHowMuch.Text);
        Answer.SetSetting("txtHowMuch", txtHowMuch.Text);

        int val = -1;
        decimal footprintVal = 0;
        try
        {
            val = int.Parse(txtHowMuch.Text);
        }
        catch(FormatException)
        {
        }

        if (val >= 0)
        {
            if (ddlHowMuch.SelectedValue == "lbs")
            {
                footprintVal = val * new Decimal(0.4536);
            }
            else
            {
                footprintVal = val;
            }

            //get the amee record if it exists
            CarbonExchange.Bll.AmeeProfileItemFactory factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
            CarbonExchange.Bll.AmeeProfileItem tmp = EnsureProfileItem(Answer.SingleItem.Name, factory, new CarbonExchange.Bll.AmeeDataCatagoryFactory(), "/home/waste", "type=general household");

            bool save = false;
            if (tmp.GetValue<string>("savingtype") != "landfill")
            {
                save = true;
                tmp.SetValue<string>("savingType", "landfill");
            }

            if (tmp.GetValue<decimal>("kgPerMonth") != footprintVal)
            {
                save = true;
                tmp.SetValue<decimal>("kgPerMonth", footprintVal);
            }

            if (save)
            {
                factory.Save(tmp);
                Answer.SetSingleItem(tmp);
            }
            return true;
        }
        else
        {
            return false;
        }
    }

}
